package com.xzc.datastruc.ujava.c07._1_introduction;

import javax.swing.*;
import java.awt.*;

public class FractalStar extends JPanel {
    private int x = 200;
    private int y = 200;

    public void display() {
        x++;
        y++;
        this.repaint();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        drawFractal(g, 250, 250, 500);
    }

    void drawFractal(Graphics g, int xCenter, int yCenter, int boundingDim) {
        int side = boundingDim / 2;
        if (side < 1)
            return;
        int left = xCenter - side / 2;
        int top = yCenter - side / 2;
        int right = xCenter + side / 2;
        int bottom = yCenter + side / 2;

        drawFractal(g, left, top, boundingDim / 2);
        drawFractal(g, left, bottom, boundingDim / 2);
        drawFractal(g, right, top, boundingDim / 2);
        drawFractal(g, right, top, boundingDim / 2);

        System.out.println(left + " " + top + " " + right + " " + bottom);
        g.fillRect(left, top, right - left, bottom - top);
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        FractalStar jp = new FractalStar();

        jf.setBounds(0, 0, 500, 500);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(jp);
        jf.setVisible(true);

        while (true) {
            jp.display();

            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
